package com.pilipili.zeus.eduService.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduCourseDescription;
import com.pilipili.zeus.eduService.entity.excel.DescriptionExcelData;
import com.pilipili.zeus.eduService.service.EduCourseDescriptionService;

/**
 * @author Unlimited_Blade_Works
 */
public class DescriptionListener extends AnalysisEventListener<DescriptionExcelData> {
    public EduCourseDescriptionService descriptionService;

    public DescriptionListener() {
    }

    public DescriptionListener(EduCourseDescriptionService descriptionService) {
        this.descriptionService = descriptionService;
    }

    //按行读取Excel内容
    @Override
    public void invoke(DescriptionExcelData descriptionExcelData, AnalysisContext analysisContext) {
        if (descriptionExcelData == null) {
            throw new MyException(20001, "文件数据为空");
        }
        EduCourseDescription description = this.isExistDescription(descriptionService, descriptionExcelData.getId());
        if (description == null) {
            description = new EduCourseDescription();
            description.setId(descriptionExcelData.getId());
            description.setDescription(descriptionExcelData.getDescription());
            descriptionService.save(description);

        }
    }
    private EduCourseDescription isExistDescription(EduCourseDescriptionService descriptionService, String id){
        QueryWrapper<EduCourseDescription> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        return descriptionService.getOne(wrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
